package com.gzxx.service.impl;

import com.gzxx.domain.po.SysUserRole;
import com.gzxx.mapper.SysUserRoleMapper;
import com.gzxx.service.SysUserRoleService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * @Author pengpdx
 * @Date 2020/7/29 15:03
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Transactional(readOnly = true, rollbackFor = Exception.class, timeout = 5)
public class SysUserRoleServiceImpl implements SysUserRoleService {

    private final SysUserRoleMapper sysUserRoleMapper;

    /**
     * 修改权限
     * @param list
     */
    @Override
    @Transactional(rollbackFor = Exception.class, timeout = 5)
    public void updateUserRoles(List<SysUserRole> list) {
        if (!CollectionUtils.isEmpty(list) && list.size() > 0){
            int deteleNum = sysUserRoleMapper.deleteByUserId(list.get(0).getUserId());
            if (deteleNum > 0){
                sysUserRoleMapper.insertBach(list);
            }
        }
    }

    /**
     * 修改用户角色
     */
    @Override
    @Transactional(rollbackFor = Exception.class, timeout = 5)
    public void updateUserRole(SysUserRole userRole) {
        if (!ObjectUtils.isEmpty(userRole)) {
            int deleteNum = sysUserRoleMapper.deleteByUserId(userRole.getUserId());
            if (deleteNum > 0) {
                sysUserRoleMapper.insert(userRole);
            }
        }
    }
}
